Sketch in 'update' option for GUI.
authorrobertl <robertl>
Mon, 27 Jul 2009 15:36:30 +0000 (15:36 +0000)
committerrobertl <robertl>
Mon, 27 Jul 2009 15:36:30 +0000 (15:36 +0000)
gui/app.pro
gui/upgrade.cpp [new file with mode: 0644]
gui/upgrade.h [new file with mode: 0644]
gui/upgrade.ui [new file with mode: 0644]

index 194fb6bd69467e2504de61572c36d38ccf8be484..a78f585d65c55589a5b3cc3ed72177504ad5ff24 100755 (executable)
@@ -1,4 +1,4 @@
-# $Id: app.pro,v 1.4 2009/07/23 03:22:23 robertl Exp $
+# $Id: app.pro,v 1.5 2009/07/27 15:36:30 robertl Exp $
 #
 
 #CONFIG += qt debug console
@@ -40,6 +40,7 @@ FORMS += wayptsui.ui
 FORMS += rttrkui.ui
 FORMS += miscfltui.ui
 FORMS += gmapui.ui
+FORMS += upgrade.ui
 
 SOURCES += advdlg.cpp
 SOURCES += dpencode.cpp
@@ -58,7 +59,7 @@ SOURCES += optionsdlg.cpp
 SOURCES += processwait.cpp
 SOURCES += filterwidgets.cpp
 SOURCES += filterdlg.cpp
-
+SOURCES += upgrade.cpp
 
 HEADERS += mainwindow.h
 HEADERS += map.h
@@ -77,6 +78,7 @@ HEADERS += processwait.h
 HEADERS += filterwidgets.h
 HEADERS += filterdata.h
 HEADERS += setting.h
+HEADERS += upgrade.h
 
 TRANSLATIONS += gpsbabelfe_de.ts
 TRANSLATIONS += gpsbabelfe_es.ts
diff --git a/gui/upgrade.cpp b/gui/upgrade.cpp
new file mode 100644 (file)
index 0000000..4ba06e6
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+    Copyright (C) 2009  Robert Lipe, robertlipe@gpsbabel.org
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
+
+ */
+
+
+#include "upgrade.h"
+#include "mainwindow.h"
+#include "ui_upgrade.h"
+
+#include <QHttp>
+#include <QUrl>
+#include <QMessageBox>
+#include <QFile>
+#include <QDomDocument>
+#include <QDomNode>
+#include <QDomElement>
+#include <QDomNodeList>
+
+Upgrade::Upgrade(QWidget *parent) :
+    QDialog(parent),
+    m_ui(new Ui::Upgrade)
+{
+    m_ui->setupUi(this);
+}
+
+Upgrade::~Upgrade()
+{
+    delete m_ui;
+}
+
+void Upgrade::changeEvent(QEvent *e)
+{
+    QDialog::changeEvent(e);
+    switch (e->type()) {
+    case QEvent::LanguageChange:
+        m_ui->retranslateUi(this);
+        break;
+    default:
+        break;
+    }
+}
+
+Upgrade::updateStatus Upgrade::checkForUpgrade()
+{
+  http = new QHttp;
+
+  connect(http, SIGNAL(requestFinished(int, bool)),
+          this, SLOT(httpRequestFinished(int, bool)));
+  connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
+          this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
+
+  http->setHost("www.gpsbabel.org");
+  httpRequestId = http->get("/updates.xml");
+
+  return Upgrade::updateUnknown;
+}
+
+void Upgrade::readResponseHeader(const QHttpResponseHeader &responseHeader)
+{
+  switch (responseHeader.statusCode()) {
+  case 200:                   // Ok
+  case 301:                   // Moved Permanently
+  case 302:                   // Found
+  case 303:                   // See Other
+  case 307:                   // Temporary Redirect
+      // these are not error conditions
+      break;
+
+  default:
+      QMessageBox::information(this, tr("HTTP"),
+                               tr("Download failed: %1.")
+                               .arg(responseHeader.reasonPhrase()));
+      httpRequestAborted = true;
+      http->abort();
+    }
+}
+
+void Upgrade::httpRequestFinished(int requestId, bool error)
+{
+  if(error or requestId != httpRequestId) {
+    return;
+  }
+
+  QString oresponse(http->readAll());
+  QDomDocument document;
+  if (!document.setContent(oresponse)) {
+    return;
+  }
+  
+  QString response("snore");
+
+  QString currentVersion =  "1.3.6"; // FIXME: Work with Khai to pry this out of MainWindow 
+  bool allowBeta = false;  // TODO: come from prefs or current version...
+
+  QDomNodeList upgrades = document.elementsByTagName("update");
+
+  for (unsigned int i = 0; i < upgrades.length(); i++) {
+    QDomNode upgradeNode = upgrades.item(i);
+    QDomElement upgrade = upgradeNode.toElement();
+
+    QString updateVersion = upgrade.attribute("version");
+    bool updateIsBeta  = upgrade.attribute("type") == "beta";
+    bool updateIsMajor = upgrade.attribute("type") == "major";
+    bool updateCandidate = updateIsMajor || (updateIsBeta && allowBeta);
+
+    if(updateVersion > currentVersion && updateCandidate) {
+      response = "<b>A new version of GPSBabel is available</b><br/ >";
+      response += "Your version is " + currentVersion + " <br />";;
+      response += "The latest version is " + updateVersion + " <br />";;
+      break;  
+    }
+  }
+
+  // FIXME: this doesn't actually write into the UI's text browser...
+  m_ui->textBrowser->setText(response);
+
+  //QMessageBox::information(this, tr("Finished"), response);
+
+}
diff --git a/gui/upgrade.h b/gui/upgrade.h
new file mode 100644 (file)
index 0000000..7754c7e
--- /dev/null
@@ -0,0 +1,66 @@
+#ifndef UPGRADE_H
+#define UPGRADE_H
+/*
+    Copyright (C) 2009  Robert Lipe, robertlipe@gpsbabel.org
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
+
+ */
+
+
+#include <QDialog>
+#include "ui_upgrade.h"
+
+class QHttp;
+class QHttpResponseHeader;
+
+namespace Ui {
+    class Upgrade;
+}
+
+class Upgrade : public QDialog {
+  Q_OBJECT
+public:
+  Upgrade(QWidget *parent = 0);
+  ~Upgrade();
+
+  typedef enum {
+    updateUnknown,
+    updateCurrent,
+    updateNeeded,
+  } updateStatus;
+
+  Upgrade::updateStatus checkForUpgrade(void);
+
+protected:
+  void changeEvent(QEvent *e);
+
+private:
+//  Ui::Upgrade *m_ui;
+  Ui_Upgrade     *m_ui;
+  QHttp *http;
+  int httpRequestId;
+  bool httpRequestAborted;
+  QString latestVersion;
+
+private slots:
+  void httpRequestFinished(int requestId, bool error);
+//  void httpStateChanged(int state);
+  void readResponseHeader(const QHttpResponseHeader &responseHeader);
+
+
+};
+
+#endif // UPGRADE_H
diff --git a/gui/upgrade.ui b/gui/upgrade.ui
new file mode 100644 (file)
index 0000000..7242ca7
--- /dev/null
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>Upgrade</class>
+ <widget class="QDialog" name="Upgrade">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>400</width>
+    <height>300</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Dialog</string>
+  </property>
+  <widget class="QDialogButtonBox" name="buttonBox">
+   <property name="geometry">
+    <rect>
+     <x>30</x>
+     <y>240</y>
+     <width>341</width>
+     <height>32</height>
+    </rect>
+   </property>
+   <property name="orientation">
+    <enum>Qt::Horizontal</enum>
+   </property>
+   <property name="standardButtons">
+    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+   </property>
+  </widget>
+  <widget class="QTextBrowser" name="textBrowser">
+   <property name="geometry">
+    <rect>
+     <x>10</x>
+     <y>10</y>
+     <width>381</width>
+     <height>211</height>
+    </rect>
+   </property>
+  </widget>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>accepted()</signal>
+   <receiver>Upgrade</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>248</x>
+     <y>254</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>157</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>rejected()</signal>
+   <receiver>Upgrade</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>316</x>
+     <y>260</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>286</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>